package com.ruoyi.StarStudio.Live.controller;

import com.alibaba.fastjson.JSONObject;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.ruoyi.StarStudio.Live.common.CONST;
import com.ruoyi.StarStudio.Live.common.ResultMap;
import com.ruoyi.StarStudio.Live.servcie.OpenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LocalRecordController {

    private static Logger logger = LoggerFactory.getLogger(LocalRecordController.class);

    @Autowired
    public OpenService openService;

    /**
     * 设置本地录制
     * @return
     */
    @PostMapping("/setLocalRecordPlan")
    public ResultMap setLocalRecordPlan(@RequestBody JSONObject jsonParam) throws HttpProcessException {
        ResultMap resultMap = new ResultMap(CONST.RESULT_CG.OK_CODE, CONST.RESULT_CG.OK_MSG);
        if (jsonParam.get("deviceId") == null||jsonParam.get("channelId") == null||jsonParam.get("period") == null||jsonParam.get("beginTime") == null||jsonParam.get("endTime") == null) {
            resultMap.setCode(CONST.RESULT_CG.PARAM_NULL_CODE);
            resultMap.setMsg(CONST.RESULT_CG.PARAM_NULL_MSG);
            logger.info("请求参数为空:" + jsonParam.toString());
            return resultMap;
        }

        openService.updateAccessToken(CONST.OPEN_SYSTEM.APPID,CONST.OPEN_SYSTEM.SECRET);
        String accessToken = CONST.OPEN_SYSTEM.accessToken;
        String channelId = jsonParam.get("channelId").toString();
        String deviceId = jsonParam.get("deviceId").toString();
        String beginTime = jsonParam.get("beginTime").toString();
        String endTime = jsonParam.get("endTime").toString();
        String period = jsonParam.get("period").toString();

        //根据设备序列号获取设备信息
        ResultMap res = openService.setLocalRecordPlan(accessToken,deviceId,channelId,beginTime,endTime,period);

        //返回结果
        return resultMap;
    }
}
